package com.csu.tingtian.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.csu.tingtian.common.CommonResponse;
import com.csu.tingtian.entity.ProduceLineItem;
import com.csu.tingtian.entity.RentLineItem;
import com.csu.tingtian.entity.RentOrders;
import com.csu.tingtian.entity.RentOrderstatus;
import com.csu.tingtian.persistence.RentLineItemMapper;
import com.csu.tingtian.persistence.RentOrdersMapper;
import com.csu.tingtian.persistence.RentOrderstatusMapper;
import com.csu.tingtian.service.RentOrderService;
import com.csu.tingtian.vo.RentOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("RentOrderService")
public class RentOrderServiceImpl implements RentOrderService {
    @Autowired
    private RentOrdersMapper rentOrdersMapper;
    @Autowired
    private RentOrderstatusMapper rentOrderstatusMapper;
    @Autowired
    private RentLineItemMapper rentLineItemMapper;
    @Override
    public CommonResponse<List<RentOrder>> getAllRentOrder(){
        List<RentOrder> rentOrderList = new ArrayList<>();
        List<RentOrders> rentOrdersList = rentOrdersMapper.selectList(null);
       List<RentOrderstatus> rentOrderstatusList = rentOrderstatusMapper.selectList(null);
        if (rentOrdersList != null) {
            for (int i = 0; i < rentOrdersList.size(); i++) {
                RentOrder rentOrder = new RentOrder();
                int rentOrderId = rentOrdersList.get(i).getRentOrderId();
                rentOrder.setRentOrderId(rentOrderId);
                rentOrder.setRentUserName(rentOrdersList.get(i).getRentUserName());
                rentOrder.setRentOrderCreateDate(rentOrdersList.get(i).getRentOrderCreateDate());
                rentOrder.setRentTotalPrice(rentOrdersList.get(i).getRentTotalPrice());
                rentOrder.setRentPaymentStatus(rentOrdersList.get(i).getRentPaymentStatus());
                rentOrder.setRentPaidDate(rentOrdersList.get(i).getRentPaidDate());
                rentOrder.setRentOrderStatus(rentOrderstatusList.get(i).getRentOrderStatus());

                QueryWrapper<RentLineItem> queryWrapper = new QueryWrapper<>();
                rentOrder.setRentLineItemList(rentLineItemMapper.selectList(queryWrapper.eq("rentOrderId",rentOrderId)));
                rentOrderList.add(rentOrder);
            }
            return CommonResponse.createForSuccess(rentOrderList);
        }
        else
            return CommonResponse.createForError("数据库中找不到农场租赁订单信息");
    }

    @Override
    public CommonResponse<List<RentOrder>> getRentOrderByRentUserName(String rentUserName) {
        List<RentOrder> rentOrderList = new ArrayList<>();
        QueryWrapper<RentOrders> rentOrdersQueryWrapper = new QueryWrapper<>();
        List<RentOrders> rentOrdersList = rentOrdersMapper.selectList(rentOrdersQueryWrapper.eq("rentUserName",rentUserName));
        if (rentOrdersList != null){
            for (int i = 0; i < rentOrdersList.size(); i++) {
                RentOrder rentOrder = new RentOrder();
                int orderid = rentOrdersList.get(i).getRentOrderId();
                rentOrder.setRentOrderId(rentOrdersList.get(i).getRentOrderId());
                rentOrder.setRentUserName(rentOrdersList.get(i).getRentUserName());
                rentOrder.setRentOrderCreateDate(rentOrdersList.get(i).getRentOrderCreateDate());
                rentOrder.setRentTotalPrice(rentOrdersList.get(i).getRentTotalPrice());
                rentOrder.setRentPaymentStatus(rentOrdersList.get(i).getRentPaymentStatus());
                rentOrder.setRentPaidDate(rentOrdersList.get(i).getRentPaidDate());
                RentOrderstatus rentOrderstatus = rentOrderstatusMapper.selectById(orderid);
                rentOrder.setRentOrderStatus(rentOrderstatus.getRentOrderStatus());

                QueryWrapper<RentLineItem> queryWrapper = new QueryWrapper<>();
                rentOrder.setRentLineItemList(rentLineItemMapper.selectList(queryWrapper.eq("rentOrderId",orderid)));

                rentOrderList.add(rentOrder);
            }
            return CommonResponse.createForSuccess(rentOrderList);
        }
        else
            return CommonResponse.createForError("服务器异常，未查询到用户名为 " + rentUserName + "的农场租赁订单。");
    }

    @Override
    public CommonResponse<RentOrder> getRentOrderByRentOrderId(int rentOrderId) {
        RentOrder rentOrder = new RentOrder();

        RentOrders rentOrders = rentOrdersMapper.selectById(rentOrderId);
        if (rentOrders != null) {
            RentOrderstatus rentOrderstatus = rentOrderstatusMapper.selectById(rentOrderId);

            rentOrder.setRentOrderId(rentOrders.getRentOrderId());
            rentOrder.setRentUserName(rentOrders.getRentUserName());
            rentOrder.setRentOrderCreateDate(rentOrders.getRentOrderCreateDate());
            rentOrder.setRentTotalPrice(rentOrders.getRentTotalPrice());
            rentOrder.setRentPaymentStatus(rentOrders.getRentPaymentStatus());
            rentOrder.setRentPaidDate(rentOrders.getRentPaidDate());

            rentOrder.setRentOrderStatus(rentOrderstatus.getRentOrderStatus());
            QueryWrapper<RentLineItem> queryWrapper = new QueryWrapper<>();
            rentOrder.setRentLineItemList(rentLineItemMapper.selectList(queryWrapper.eq("rentOrderId",rentOrderId)));

            return CommonResponse.createForSuccess(rentOrder);
        }
        else
            return CommonResponse.createForError("服务器异常，未查询到rentOrderId为 " + rentOrderId + "的订单");
    }

    @Override
    public CommonResponse<RentOrder> insertRentOrder(RentOrder rentOrder) {
        RentOrders rentOrders = new RentOrders();
        RentOrderstatus rentOrderstatus = new RentOrderstatus();

        int rentOrderId = getNextRentOrderId();
        rentOrders.setRentOrderId(rentOrderId);
        rentOrders.setRentUserName(rentOrder.getRentUserName());
        rentOrders.setRentOrderCreateDate(getSystemTime());
        rentOrders.setRentTotalPrice(rentOrder.getRentTotalPrice());
        rentOrders.setRentPaymentStatus(rentOrder.getRentPaymentStatus());
        rentOrders.setRentPaidDate(rentOrder.getRentPaidDate());

        rentOrderstatus.setRentOrderId(rentOrderId);
        rentOrderstatus.setRentOrderStatus(rentOrder.getRentOrderStatus());

        int theNumOfRentLineItemList = rentOrder.getRentLineItemList().size();
        for (int i = 0; i < theNumOfRentLineItemList; i++) {
            RentLineItem rentLineItem = new RentLineItem();
            rentLineItem.setRentOrderId(rentOrderId);
            rentLineItem.setRentItemId(rentOrder.getRentLineItemList().get(i).getRentItemId());
            rentLineItem.setRentQuantity(rentOrder.getRentLineItemList().get(i).getRentQuantity());
            rentLineItem.setRentUntiArea(rentOrder.getRentLineItemList().get(i).getRentUntiArea());
            rentLineItem.setFarmName(rentOrder.getRentLineItemList().get(i).getFarmName());

            rentLineItemMapper.insert(rentLineItem);
        }
        //插入
        int i = rentOrdersMapper.insert(rentOrders);
        rentOrderstatusMapper.insert(rentOrderstatus);
        if (i > -1){
            return CommonResponse.createForSuccess("插入租赁订单成功。",rentOrder);
        }
        else
            return CommonResponse.createForError("服务器异常，插入订单失败。");
    }

    private int getNextRentOrderId() {
        int orderid = rentOrdersMapper.selectCount(null);
        orderid++;
        return orderid;
    }
    private String getSystemTime() {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time= sdf.format( new Date());
        return time;
    }

    @Override
    public CommonResponse<RentOrder> deleteRentOrderByRentOrderId(int rentOrderId) {
        RentOrder rentOrder = getRentOrderByRentOrderId(rentOrderId).getData();
        int i = rentOrdersMapper.deleteById(rentOrderId);

        //删除ProduceLineItem中的内容
        QueryWrapper<RentLineItem> queryWrapper = new QueryWrapper<>();
        rentLineItemMapper.delete(queryWrapper.eq("rentOrderId",rentOrderId));
        if (i > -1) {//删除成功返回0
            rentOrderstatusMapper.deleteById(rentOrderId);
            return CommonResponse.createForSuccess("删除订单号为 " + rentOrderId +"的订单成功。",rentOrder);
        }
        else
            return CommonResponse.createForError("服务器异常，删除该租赁订单失败。");

    }

    @Override
    public CommonResponse<RentOrder> updateRentOrder(RentOrder rentOrder) {
        RentOrders rentOrders = new RentOrders();
        RentOrderstatus rentOrderstatus = new RentOrderstatus();

        int rentOrderId = rentOrder.getRentOrderId();
        rentOrders.setRentOrderId(rentOrderId);
        rentOrders.setRentUserName(rentOrder.getRentUserName());
        rentOrders.setRentOrderCreateDate(getSystemTime());
        rentOrders.setRentTotalPrice(rentOrder.getRentTotalPrice());
        rentOrders.setRentPaymentStatus(rentOrder.getRentPaymentStatus());
        rentOrders.setRentPaidDate(rentOrder.getRentPaidDate());

        rentOrderstatus.setRentOrderId(rentOrder.getRentOrderId());
        rentOrderstatus.setRentOrderStatus(rentOrder.getRentOrderStatus());

        int theNumOfRentLineItemList = rentOrder.getRentLineItemList().size();

        try{ for (int i = 0; i < theNumOfRentLineItemList; i++) {
            RentLineItem rentLineItem = new RentLineItem();
            rentLineItem.setRentOrderId(rentOrderId);
            rentLineItem.setRentItemId(rentOrder.getRentLineItemList().get(i).getRentItemId());
            rentLineItem.setRentQuantity(rentOrder.getRentLineItemList().get(i).getRentQuantity());
            rentLineItem.setRentUntiArea(rentOrder.getRentLineItemList().get(i).getRentUntiArea());
            rentLineItem.setFarmName(rentOrder.getRentLineItemList().get(i).getFarmName());

            QueryWrapper<RentLineItem> queryWrapper = new QueryWrapper<>();
            rentLineItemMapper.update(rentLineItem,queryWrapper.eq("rentOrderId",rentLineItem));
        }}catch (Exception e){
            e.printStackTrace();
        }
        //更新
        int j = rentOrdersMapper.updateById(rentOrders);
        if (j > -1) {
            rentOrderstatusMapper.updateById(rentOrderstatus);
            return CommonResponse.createForSuccess("更新订单号为" + rentOrder.getRentOrderId() +"成功",rentOrder);
        }
        else
            return CommonResponse.createForError("服务器异常，更新订单失败。");
    }
}
